var mongoose = require('../modules/database');
var Schema = mongoose.Schema;
var schema = new Schema({
    studentNo: {
        type: Number,

    },
    password: {
        type: String,
        default: 123456,
    },
    name: {
        type: String,
        require: true,
        default: ""
    },
    sex: {
        type: String,
        default: "",
    },
    age: {
        type: Number,
        default: ""
    },
    identity: {
        type: Number,
        default: 1,
    },
    phone: {
        type: Number,
        default: ""
    },
    dormAddress: {
        type: String,
        default: ""
    },
    dormNo: {
        type: Number,
        default: ""
    },
    ID: {
        type: String,
        default: ""
    },
    weixin: {
        type: Number,
        default: ""
    },
    qq: {
        type: Number,
        default: ""
    },
    email: {
        type: String,
        default: ""
    },
    school: {
        type: String,
        default: ""
    },
    major: {
        type: String,
        default: ""
    },
    education: {
        type: String,
        default: ""
    },
    joinDate: {
        type: String,
        default: ""
    },
    gradDate: {
        type: String,
        default: ""
    },
    studyStatus: {
        type: String,
        default: "",
    },
    introTeacher: {
        type: String,
        default: ""
    },
    chargeTeacher: {
        type: String,
        default: ""
    },
    teachTeacher: {
        type: String,
        default: ""
    },
    className: {
        type: String,
        default: ""
    },
    classNameId: {
        type: String,
        default: ""
    },
    pay: {
        type: String,
        default: ""
    },
})

schema.statics = {
    //登录
    login: function (data, cb) {
        var filter = {
            studentNo: data.account,
            password: data.password
        };
        Model.findOne(filter, function (err, result) {
            if (err) {
                return cb({ code: 201, message: err });
            }
            if (!result) {
                return cb({ code: 201, message: '账号或密码错误！' });
            }
            return cb(null, { code: 200, result });
        })
    },
    //更改密码
    alterpwd: function (data, cb) {
        Model.findOneAndUpdate({ studentNo: data.account }, { $set: { password: data.password } }, function (err, result) {
            if (err) {
                return cb({ code: 201, message: '重置密码失败！' });
            }
            return cb(null, { code: 200, message: result });
        })
    },
    //添加学生
    addstudent: function (data, cb) {
        var filter = { studentNo: data.studentNo }
        Model.findOne().or(filter).exec(function (err, result) {
            if (err) {
                return cb({ code: 201, message: err });
            }

            if (result) {
                return cb({ code: 201, message: "注册的学号已存在" });
            } else {
                Model.insertMany(data, function (err, result) {
                    if (err) {
                        return cb({ code: 201, message: err });
                    }
                    return cb(null, result);
                })
            }
        })

    },
    //修改个人资料
    alterdata: function (data, cb) {
        var update = {
            //20
            name: data.name,
            age: data.age,
            sex: data.sex,
            phone: data.phone,
            ID: data.ID,
            weixin: data.weixin,
            qq: data.qq,
            email: data.email,
            school: data.school,
            major: data.major,
            education: data.education,
            joinDate: data.joinDate,
            gradDate: data.gradDate,
            studyStatus: data.studyStatus,
            introTeacher: data.introTeacher,
            chargeTeacher: data.chargeTeacher,
            teachTeacher: data.teachTeacher,
            belongClass: data.belongClass,
            dormAddress: data.dormAddress,
            dormNo: data.dormNo
        };

        Model.findOneAndUpdate({ studentNo: data.studentNo }, { $set: update }, { new: true }, function (err, result) {
            if (err) {
                return cb({ code: 201, message: '修改资料失败！' });
            }
            return cb(null, { code: 200, message: result });
        })
    },
    //删除个人资料
    deletestudent:function(filter,cb){
        
        Model.remove(filter,function(err,result){
            if (err) {
                return cb({ code: 201, message: '删除信息失败！' });
            }
            return cb(null, { code: 200, message: result });
        })
    }
};

var Model = mongoose.model('students', schema);
module.exports = Model;